﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using SobFS.IDAL;
using SobFS.Common.Database;

namespace SobFS.MySQLDAL
{
    /// <summary>
    /// 数据访问类:SysUser
    /// </summary>
    public partial class SysUser : ISysUser
    {
        public SysUser()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("Id", "SysUser");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SysUser");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SobFS.Model.SysUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SysUser(");
            strSql.Append("OrganizationId,LoginName,Password,RealName,NickName,Email,Mobile,QQ,Blog,Weibo,IsObsoleted,IsEnabled,Description)");
            strSql.Append(" values (");
            strSql.Append("@OrganizationId,@LoginName,@Password,@RealName,@NickName,@Email,@Mobile,@QQ,@Blog,@Weibo,@IsObsoleted,@IsEnabled,@Description)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@OrganizationId", MySqlDbType.Int32,11),
					new MySqlParameter("@LoginName", MySqlDbType.VarChar,50),
					new MySqlParameter("@Password", MySqlDbType.VarChar,100),
					new MySqlParameter("@RealName", MySqlDbType.VarChar,50),
					new MySqlParameter("@NickName", MySqlDbType.VarChar,50),
					new MySqlParameter("@Email", MySqlDbType.VarChar,100),
					new MySqlParameter("@Mobile", MySqlDbType.VarChar,50),
					new MySqlParameter("@QQ", MySqlDbType.VarChar,50),
					new MySqlParameter("@Blog", MySqlDbType.VarChar,255),
					new MySqlParameter("@Weibo", MySqlDbType.VarChar,255),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text)};
            parameters[0].Value = model.OrganizationId;
            parameters[1].Value = model.LoginName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.RealName;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.Mobile;
            parameters[7].Value = model.QQ;
            parameters[8].Value = model.Blog;
            parameters[9].Value = model.Weibo;
            parameters[10].Value = model.IsObsoleted;
            parameters[11].Value = model.IsEnabled;
            parameters[12].Value = model.Description;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SobFS.Model.SysUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SysUser set ");
            strSql.Append("OrganizationId=@OrganizationId,");
            strSql.Append("LoginName=@LoginName,");
            strSql.Append("Password=@Password,");
            strSql.Append("RealName=@RealName,");
            strSql.Append("NickName=@NickName,");
            strSql.Append("Email=@Email,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("QQ=@QQ,");
            strSql.Append("Blog=@Blog,");
            strSql.Append("Weibo=@Weibo,");
            strSql.Append("IsObsoleted=@IsObsoleted,");
            strSql.Append("IsEnabled=@IsEnabled,");
            strSql.Append("Description=@Description");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@OrganizationId", MySqlDbType.Int32,11),
					new MySqlParameter("@LoginName", MySqlDbType.VarChar,50),
					new MySqlParameter("@Password", MySqlDbType.VarChar,100),
					new MySqlParameter("@RealName", MySqlDbType.VarChar,50),
					new MySqlParameter("@NickName", MySqlDbType.VarChar,50),
					new MySqlParameter("@Email", MySqlDbType.VarChar,100),
					new MySqlParameter("@Mobile", MySqlDbType.VarChar,50),
					new MySqlParameter("@QQ", MySqlDbType.VarChar,50),
					new MySqlParameter("@Blog", MySqlDbType.VarChar,255),
					new MySqlParameter("@Weibo", MySqlDbType.VarChar,255),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text),
					new MySqlParameter("@Id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.OrganizationId;
            parameters[1].Value = model.LoginName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.RealName;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.Mobile;
            parameters[7].Value = model.QQ;
            parameters[8].Value = model.Blog;
            parameters[9].Value = model.Weibo;
            parameters[10].Value = model.IsObsoleted;
            parameters[11].Value = model.IsEnabled;
            parameters[12].Value = model.Description;
            parameters[13].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysUser ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysUser ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SobFS.Model.SysUser GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrganizationId,LoginName,Password,RealName,NickName,Email,Mobile,QQ,Blog,Weibo,IsObsoleted,IsEnabled,Description from SysUser ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            SobFS.Model.SysUser model = new SobFS.Model.SysUser();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return GetModelByDataRow(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        private Model.SysUser GetModelByDataRow(DataRow datarow)
        {
            SobFS.Model.SysUser model = null;
            if (datarow != null && datarow.ItemArray != null && datarow.ItemArray.Length > 0)
            {
                model = new SobFS.Model.SysUser();
                if (datarow["Id"] != null && datarow["Id"].ToString() != "")
                {
                    model.Id = int.Parse(datarow["Id"].ToString());
                }
                if (datarow["OrganizationId"] != null && datarow["OrganizationId"].ToString() != "")
                {
                    model.OrganizationId = int.Parse(datarow["OrganizationId"].ToString());
                }
                if (datarow["LoginName"] != null && datarow["LoginName"].ToString() != "")
                {
                    model.LoginName = datarow["LoginName"].ToString();
                }
                if (datarow["Password"] != null && datarow["Password"].ToString() != "")
                {
                    model.Password = datarow["Password"].ToString();
                }
                if (datarow["RealName"] != null && datarow["RealName"].ToString() != "")
                {
                    model.RealName = datarow["RealName"].ToString();
                }
                if (datarow["NickName"] != null && datarow["NickName"].ToString() != "")
                {
                    model.NickName = datarow["NickName"].ToString();
                }
                if (datarow["Email"] != null && datarow["Email"].ToString() != "")
                {
                    model.Email = datarow["Email"].ToString();
                }
                if (datarow["Mobile"] != null && datarow["Mobile"].ToString() != "")
                {
                    model.Mobile = datarow["Mobile"].ToString();
                }
                if (datarow["QQ"] != null && datarow["QQ"].ToString() != "")
                {
                    model.QQ = datarow["QQ"].ToString();
                }
                if (datarow["Blog"] != null && datarow["Blog"].ToString() != "")
                {
                    model.Blog = datarow["Blog"].ToString();
                }
                if (datarow["Weibo"] != null && datarow["Weibo"].ToString() != "")
                {
                    model.Weibo = datarow["Weibo"].ToString();
                }
                if (datarow["IsObsoleted"] != null && datarow["IsObsoleted"].ToString() != "")
                {
                    if ((datarow["IsObsoleted"].ToString() == "1") || (datarow["IsObsoleted"].ToString().ToLower() == "true"))
                    {
                        model.IsObsoleted = true;
                    }
                    else
                    {
                        model.IsObsoleted = false;
                    }
                }
                if (datarow["IsEnabled"] != null && datarow["IsEnabled"].ToString() != "")
                {
                    if ((datarow["IsEnabled"].ToString() == "1") || (datarow["IsEnabled"].ToString().ToLower() == "true"))
                    {
                        model.IsEnabled = true;
                    }
                    else
                    {
                        model.IsEnabled = false;
                    }
                }
                if (datarow["Description"] != null && datarow["Description"].ToString() != "")
                {
                    model.Description = datarow["Description"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrganizationId,LoginName,Password,RealName,NickName,Email,Mobile,QQ,Blog,Weibo,IsObsoleted,IsEnabled,Description ");
            strSql.Append(" FROM SysUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SysUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from SysUser T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "SysUser";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

